package com.yegs.admin.business.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yegs.admin.business.entity.BaseMenu;
import com.yegs.admin.business.entity.BaseRole;
import com.yegs.admin.business.entity.BaseRoleMenu;
import com.yegs.admin.business.mapper.BaseMenuMapper;
import com.yegs.admin.business.mapper.BaseRoleMenuMapper;
import com.yegs.admin.business.service.IBaseMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yegs
 * @since 2020-08-28
 */
@Service
public class BaseMenuServiceImpl extends ServiceImpl<BaseMenuMapper, BaseMenu> implements IBaseMenuService {
    
    @Autowired
    private BaseMenuMapper baseMenuMapper;

    @Autowired
    private BaseRoleMenuMapper baseRoleMenuMapper;
    
    
    public void saveMenus(BaseRole baseRole) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        BaseRoleMenu baseRoleMenu = new BaseRoleMenu();
        baseRoleMenu.setRoleId(baseRole.getRoleId());
        queryWrapper.setEntity(baseRoleMenu);
        baseRoleMenuMapper.delete(queryWrapper);
        for(BaseMenu baseMenu:baseRole.getMenus()){
            queryWrapper.setEntity(baseMenu);
            BaseMenu one = baseMenuMapper.selectOne(queryWrapper);
            if(one==null){
                throw new Exception("错误菜单ID");
            }else {
                baseRoleMenu = new BaseRoleMenu();
                baseRoleMenu.setRoleId(baseRole.getRoleId());
                baseRoleMenu.setMenuId(baseMenu.getMenuId());
                baseRoleMenuMapper.insert(baseRoleMenu);
            }
        }
    }
    public List<BaseMenu> getTree(){
        return baseMenuMapper.getTree();
    }

}
